#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import os
import importlib
from pdc_param import PdcConst


class PdcPluginManager:

    def __init__(self, param):
        self.param = param

        self._pluginList = []
        for fn in os.listdir(PdcConst.pluginsDir):
            if fn == "__init__.py":
                continue
            pluginId = fn.replace(".py", "")
            mod = importlib.import_module("plugins." + pluginId)
            self._pluginList.append(mod.Plugin(pluginId))

        self._bRunning = False

    def start(self):
        for p in self._pluginList:
            ctx = PdcPluginContext(p)
            p.start(ctx)
        self._bRunning = True

    def stop(self):
        for p in reversed(self._pluginList):
            p.stop()
        self._bRunning = False

    def dispose(self):
        assert not self._bRunning


class PdcPluginBase:

    def __init__(self):
        self._id = None

    def get_id(self):
        return self._id


class PdcPluginContext:

    def __init__(self, plugin):
        self._cacheDir = os.path.join(PdcConst.cacheDir, plugin.get_id())
    
    def get_cache_dir(self):
        return self._cacheDir
